package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//套餐管理
@RestController
@Slf4j
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;

    @Autowired
    private SetmealDishService setmealDishService;
//新增套餐
@PostMapping
//清除setmealCache名称下,所有的缓存数据
@CacheEvict(value = "setmealCache",allEntries = true)
    public R save(@RequestBody SetmealDto setmealDto){
    log.info("套餐信息",setmealDto);
    setmealService.saveWithDish(setmealDto);
    return R.success("新增成功");
}
//分页查询
    @GetMapping("/page")
    public R page(int page,int pageSize,String name){
        Page<SetmealDto> setmealDtoPage = setmealService.pageByName(page, pageSize, name);
        return R.success(setmealDtoPage);
    }

//    删除套餐
@DeleteMapping
@CacheEvict(value = "setmealCache",allEntries = true) //清除setmealCache名称下,所有的缓存数据
public R<String> delete(@RequestParam List<Long> ids){
    log.info("ids:{}",ids);
    setmealService.removeWithDish(ids);
    return R.success("套餐数据删除成功");
}

//   根据id查询菜品信息和分类信息
    @GetMapping("/{id}")
    public R get(@PathVariable Long id){
        SetmealDto setmealDto = setmealService.getByIdWithDish(id);
        return R.success(setmealDto);
    }
//    修改套餐
    @PutMapping
    public  R update(@RequestBody SetmealDto setmealDto){
    log.info(setmealDto.toString());
        setmealService.updateWithDish(setmealDto);
    return R.success("修改菜品成功");
    }

//    套餐的起售和停售
    @PostMapping("/status/{status}")
    public R updateStatus(@PathVariable Integer status,@RequestParam List<Long> ids){
    setmealService.updateStatus(status,ids);
    return R.success("套餐状态修改成功");
    }
//根据条件查询套餐数据
    @GetMapping("/list")
    @Cacheable(value = "setmealCache",key = "#setmeal.categoryId+'_'+#setmeal.status")
    public R list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus()!=null,Setmeal::getStatus,setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> list = setmealService.list();
        return R.success(list);

    }


}
